package com.mzw.huaxiarealestatebackend.service.impl;
import cn.dev33.satoken.secure.SaSecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzw.huaxiarealestatebackend.common.utils.DateFormat;
import com.mzw.huaxiarealestatebackend.dao.ClientDao;
import com.mzw.huaxiarealestatebackend.dao.UserDao;
import com.mzw.huaxiarealestatebackend.entity.BackPage;
import com.mzw.huaxiarealestatebackend.entity.Client;
import com.mzw.huaxiarealestatebackend.entity.User;
import com.mzw.huaxiarealestatebackend.service.ClientService;
import com.mzw.huaxiarealestatebackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * (Client)表服务实现类
 *
 * @author makejava
 * @since 2023-12-11 16:21:54
 */
@Service("clientService")
public class ClientServiceImpl implements ClientService {
    @Resource
    private ClientDao clientDao;
    @Autowired
    private UserDao userDao;

    @Value("${mzw.password-key}")
    private String passwordKey;
    /**
     * 查询所有
     * @return 实例对象列表
     */
    @Override
    public List<Client> queryAll() {
        List<Client> clients = clientDao.selectList(null);
        return clients;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param cltNo 主键
     * @return 实例对象
     */
    @Override
    public Client queryById(String cltNo) {
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clt_no", cltNo);
        Client client = clientDao.selectOne(queryWrapper);
        return client;
    }


    /**
     * 模糊查询
     * @param client
     * @return
     */
    @Override
    public List<Client> queryByCondition(Client client) {
        LambdaQueryWrapper<Client> lqw = new LambdaQueryWrapper<>();

        lqw.and(wrapper ->
                wrapper.eq(client.getCltNo() != null, Client::getCltNo, client.getCltNo())
                        .like(client.getCltTel() != null, Client::getCltTel, client.getCltTel())
                        .like(client.getCltName() != null, Client::getCltName, client.getCltName())
                        .like(client.getCltQq() != null, Client::getCltQq, client.getCltQq())
                        .like(client.getCltEmail() != null, Client::getCltEmail, client.getCltEmail())
                        .like(client.getCltUnit() != null, Client::getCltUnit, client.getCltUnit())
        );

        List<Client> clients = this.clientDao.selectList(lqw);
        return clients;
    }


    /**
     * 新增数据
     *
     * @param client 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Client client) {
        client.setCltNo("C" + new Random().nextInt(100000));
        int line = clientDao.insert(client);
        User user = new User();
        user.setUserId(client.getCltNo());
        user.setUserName(client.getCltName());
        String ciphertext = SaSecureUtil.aesEncrypt(passwordKey, "123456");
        user.setUserPassword(ciphertext);
        user.setRegisterTime(DateFormat.getNowDateFormat());
        user.setUserRole("客户");
        System.out.println(user);
        int insert = this.userDao.insert(user);
        return line > 0 && insert > 0;
    }

    /**
     * 修改数据
     *
     * @param
     * @return 实例对象
     */
    @Override
    public boolean update(Client client) {
        int i = clientDao.update(client);
        return i > 0;
    }
}
